package com.example.demo.netty;

import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.*;


@Service
public class Conn2YServerService implements ApplicationContextAware {

    private static final Logger logger = LoggerFactory.getLogger(Conn2YServerService.class);

    private ApplicationContext applicationContext;



    private Conn2YServerClient conn2YServerClient;

    private List<String> channelList = new ArrayList<String>();

    private Map<String, String> yundaMap = new ConcurrentHashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;

        this.startService();
        this.startService("127.0.0.1",12306);
        this.startService("172.16.0.106",12306);
        this.startService("127.0.0.1",1002);


        System.out.println("conn2YServerService Start.......SUCCESS");


    }
    private void startService() {
        this.conn2YServerClient = new Conn2YServerClient(this.applicationContext, this);
        try {
            this.conn2YServerClient.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void startService(String serverIp ,int scanDetailPort) {
        Channel channel = null;
        try {
            channel = this.conn2YServerClient.getAndCreateChannel(serverIp + ":" + scanDetailPort);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
